package com.test.demo1.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.config.Config;
import org.redisson.config.ReadMode;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SubscriptionMode;
import org.redisson.config.TransportMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class RedissonConfig {

    @Bean
    @Primary
    public RedissonClient redissonClient() {
        Config config = new Config();

        // 设置编解码器
        config.setCodec(new JsonJacksonCodec());

        // 配置线程参数
        config.setThreads(16);
        config.setNettyThreads(32);
        config.setTransportMode(TransportMode.NIO);

        // 配置哨兵模式
       config.useSentinelServers()
                .setMasterName("mymaster")
                .setReadMode(ReadMode.SLAVE)
                .setSubscriptionMode(SubscriptionMode.SLAVE)
                .addSentinelAddress(
                        "redis://192.168.242.131:26379"
                )
                .setDatabase(0)
                .setPassword("Tengzhiyang@123")
                .setMasterConnectionPoolSize(64)
                .setSlaveConnectionPoolSize(64)
                .setSubscriptionConnectionPoolSize(50)
                .setSentinelPassword("Tengzhiyang@123")
                .setConnectTimeout(3000)
                .setTimeout(5000)
                .setRetryAttempts(3)

                .setRetryInterval(1500)
        .setCheckSentinelsList(false)  // 避免检查多个节点
        .setScanInterval(10000);      // 增加主节点刷新间隔;

        return Redisson.create(config);
    }
}
